#include "formattededitor.h"


FormattedEditor::FormattedEditor(QToolBar *newToolbar, QWidget *parent) : QTextEdit(parent)
{
	// Add toolbar.
	toolBar = newToolbar;
	bold = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/bold.png"),
				    "Bold", toolBar);
	italic = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/italic.png"),
				      "Italic", toolBar);
	underline = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/underline.png"),
					 "Underline", toolBar);
	font = new QFontComboBox(toolBar);
	fontSize = new QComboBox(toolBar);
	QActionGroup *alignGroup = new QActionGroup(toolBar);
	leftAlign = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/left_align.png"),
					 "Left align", alignGroup);
	centreAlign = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/center_align.png"),
					   "Centre align", alignGroup);
	rightAlign = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/right_align.png"),
					  "Right align", alignGroup);
	justifyAlign = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/just_align.png"),
					    "Justify align", alignGroup);
	bullets = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/bullets.png"),
				       "Bullets", toolBar);
	numbers = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/numbers.png"),
				       "Numbers", toolBar);
	colour = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/text_colour.png"),
				      "Text colour", toolBar);
	textBackgroundColour = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/bucket.png"),
						    "Text background colour", toolBar);
        userHighlight = new QAction(QIcon(":mainToolbarIcons/images/formattingToolbar/highlight.png"),
                                                    "User Highlighting", toolBar);

	bold->setCheckable(true);
	italic->setCheckable(true);
	underline->setCheckable(true);
        userHighlight->setCheckable(true);

	fontSize->addItems(QStringList() << "6" << "7" << "8" << "9" << "10"
			   << "11" << "12" << "14" << "16" << "18" << "20" << "22"
			   << "24" << "26" << "28" << "36" << "48" << "72");

	fontSize->setFocusPolicy(Qt::NoFocus);
	leftAlign->setCheckable(true);
	leftAlign->setChecked(true);
	centreAlign->setCheckable(true);
	rightAlign->setCheckable(true);
	justifyAlign->setCheckable(true);
	bullets->setCheckable(true);
	numbers->setCheckable(true);

	toolBar->setIconSize(QSize(16, 16));
	toolBar->addActions(QList<QAction *>() << bold << italic << underline);
	toolBar->addSeparator();
	toolBar->addWidget(font);
	toolBar->addWidget(fontSize);
	toolBar->addSeparator();
	toolBar->addActions(QList<QAction *>() << leftAlign << centreAlign
			       << rightAlign << justifyAlign);
	toolBar->addSeparator();
	toolBar->addActions(QList<QAction *>() << bullets << numbers);
	toolBar->addSeparator();
        toolBar->addActions(QList<QAction *>() << colour << textBackgroundColour << userHighlight);

	// Connect text editor and formatting signals.
	connect(bold, SIGNAL(toggled(bool)), this, SLOT(setBold(bool)));
	connect(italic, SIGNAL(toggled(bool)), this, SLOT(setFontItalic(bool)));
	connect(underline, SIGNAL(toggled(bool)), this, SLOT(setFontUnderline(bool)));
	connect(font, SIGNAL(currentFontChanged(QFont)), this, SLOT(setCurrentFont(QFont)));
	connect(fontSize, SIGNAL(currentIndexChanged(QString)), this, SLOT(setFontSize(QString)));
	connect(leftAlign, SIGNAL(toggled(bool)), this, SLOT(setLeftAlign(bool)));
	connect(centreAlign, SIGNAL(toggled(bool)), this, SLOT(setCentreAlign(bool)));
	connect(rightAlign, SIGNAL(toggled(bool)), this, SLOT(setRightAlign(bool)));
	connect(justifyAlign, SIGNAL(toggled(bool)), this, SLOT(setJustifyAlign(bool)));
	connect(bullets, SIGNAL(toggled(bool)), this, SLOT(setBulletFormatter(bool)));
	connect(numbers, SIGNAL(toggled(bool)), this, SLOT(setNumberFormatter(bool)));
	connect(colour, SIGNAL(triggered()), this, SLOT(chooseFontColour()));
	connect(textBackgroundColour, SIGNAL(triggered()), this, SLOT(chooseFontBackgroundColor()));

        //set up the highlighter
        highlighter = new userhighlighter(document());
        connect(userHighlight, SIGNAL(toggled(bool)), highlighter, SLOT(toggleHighlighting(bool)));
}

/**
  * Function detects if a file is a formatted file. We should use perhaps
  * a header (like doctype for HTML) or metadata to detect this.
  */
bool FormattedEditor::isFormattedFile(const QString fileName)
{
        return 42;
}

void FormattedEditor::setBold(const bool bold)
{
	if (bold)
		setFontWeight(QFont::Bold);

	else
		setFontWeight(QFont::Normal);
}

void FormattedEditor::setFontSize(const QString newSize)
{
	setFontPointSize(newSize.toFloat());
}

void FormattedEditor::setLeftAlign(const bool align)
{
	if (align)
		setAlignment(Qt::AlignLeft);
}

void FormattedEditor::setCentreAlign(const bool align)
{
	if (align)
		setAlignment(Qt::AlignCenter);
}

void FormattedEditor::setRightAlign(const bool align)
{
	if (align)
		setAlignment(Qt::AlignRight);
}

void FormattedEditor::setJustifyAlign(const bool align)
{
	if (align)
		setAlignment(Qt::AlignJustify);
}

void FormattedEditor::setBulletFormatter(const bool bulletsOn)
{
	if (bulletsOn)
	{
		numbers->setChecked(false);

		setAutoFormatting(QTextEdit::AutoBulletList);
	}

	else if (!numbers->isChecked())
		setAutoFormatting(QTextEdit::AutoNone);
}

void FormattedEditor::setNumberFormatter(const bool numbersOn)
{
	if (numbersOn)
	{
		bullets->setChecked(false);

		setAutoFormatting(QTextEdit::AutoBulletList);
	}

	else if (!bullets->isChecked())
		setAutoFormatting(QTextEdit::AutoNone);
}

void FormattedEditor::chooseFontColour()
{
	setTextColor(QColorDialog::getColor(textColor(), window(), "Choose font colour"));
}

void FormattedEditor::chooseFontBackgroundColor()
{
	setTextBackgroundColor(QColorDialog::getColor(textColor(), window(),
						      "Choose font background color"));
}

void FormattedEditor::saveFile()
{
	file.open(QIODevice::WriteOnly);
	file.write(toHtml().toLatin1());
}
